package com.cfushn.ddd.application.dto.res;

import com.cfushn.ddd.application.dto.CodeEnum;
import com.cfushn.ddd.application.dto.DTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * ResultDTO
 *
 * @author CFuShn 2022/6/29 11:56
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings("all")
public class ResDTO<T> extends DTO {

    private static final long serialVersionUID = 1L;

    public static final ResDTO<String> SUCCESS = ResDTO.succeed(null, "success!");

    public static final ResDTO<String> ERROR = ResDTO.failed("error!");

    protected String code;

    protected T data;

    protected String msg = "";

    public static ResDTO<String> succeed() {
        return succeed(null);
    }

    public static <T> ResDTO<T> succeed(T data) {
        return succeed(data, "success!");
    }

    public static <T> ResDTO<T> succeed(T data, String msg) {
        return succeed(data, CodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> ResDTO<T> succeed(T data, String code, String msg) {
        return new ResDTO<>(code, data, msg);
    }

    public static <T> ResDTO<T> failed() {
        return failed("error!");
    }

    public static <T> ResDTO<T> failed(String errMsg) {
        return failed(null, errMsg);
    }

    public static <T> ResDTO<T> failed(T data, String errMsg) {
        return failed(data, CodeEnum.ERROR.getCode(), errMsg);
    }

    public static <T> ResDTO<T> failed(T data, String code, String errMsg) {
        return new ResDTO<>(code, data, errMsg);
    }

    public static <T> ResDTO<T> warning(T data, String msg) {
        return new ResDTO<>(CodeEnum.WARNING.getCode(), data, msg);
    }

    public boolean isSuccess() {
        return CodeEnum.SUCCESS.getCode().equals(code);
    }
}

